using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Lexalytics;

namespace SalienceEngineDemo
{
    public partial class ctrlGetOpinionTaggedText : UserControl, DemoControl
    {
        public RichTextBox oTextBox;
        private HighlightManager oHighlighting;


        public ctrlGetOpinionTaggedText(RichTextBox oTextBox)
        {
            InitializeComponent();
            oHighlighting = new HighlightManager();
            defineHighlightColors();
            oHighlighting.CreateLegend(legendPanel);
            this.oTextBox = oTextBox;
            EnableProcessButton();            
        }

        public void processText()
        {
            try
            {
                SalienceDemo.oSalienceSession.PrepareText(this.oTextBox.Text);
                string strTaggedText = SalienceDemo.oSalienceSession.GetNamedOpinionTaggedText();
                strTaggedText = strTaggedText.Replace("lxa_", "LXA_");
                webResults.DocumentText = oHighlighting.FormatResults(strTaggedText);
            }
            catch (SalienceException e)
            {
                MessageBox.Show(e.Message, "Salience Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void defineHighlightColors()
        {
            //entities of the following types will be colored and have a key in the entities legend section.
            oHighlighting.AddHighlight("speaker",     "#00FF00"); //Green
            oHighlighting.AddHighlight("topic", "#BB44CC"); //Light Blue
            oHighlighting.AddHighlight("statement",     "#CCCCCC"); //Light Gray      

            
        }

        private void getOpinionTaggedText_VisibleChanged(object sender, EventArgs e)
        {
            webResults.DocumentText = "";
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            processText();
        }

        #region DemoControl interface methods
        public void btnOptions_Click(object sender, EventArgs e)
        {
            ((SalienceDemo)this.Parent).StartOptionsDialog((int)Dialogs.Options.OptionSections.NOTHING);
            EnableProcessButton();
        }

        public void SetFontSize()
        {
            if (this.webResults.Document.Body != null)
            {
                Graphics g = this.CreateGraphics();
                float pixels = Properties.Settings.Default.fFontSize * g.DpiX / 72;
                g.Dispose();
                this.webResults.Document.Body.Style = "font-size:" + pixels + "px;";
            }
        }

        public void EnableProcessButton()
        {
            this.btnProcess.Enabled = (oTextBox.Text.Trim().Length > 0);
        }

        #endregion
    }
}
